﻿//----------------------------------------------
// 作者:张骁晓
// 日期:2015-06-05
// 功能:变更员工信息

//依赖于/scripts/common/jquery.data.js

var formcode = getPar("formcode");

//原职务ID
var oldDutyId = "";
//原类型
var oldType = "";
///初始化信息
$(document).ready(function () {
    initInfo();//加载信息

});

//加载信息
var initInfo = function () {
    var dept_id = mini.get("dept_id");
    var duty_id = mini.get("duty_id");
    //依赖于/scripts/common/jquery.data.js
    //加载日期
    var t = mini.get("effectDate");
    t.setValue(new Date());
    var data = fnGetSingleData("t_base_employee", "id='" + getPar("eid") + "'");
    //反序列化实体对象到表单
    if (data != null && typeof (data) != "undefined" && data.length > 0) {
        //变更职务时加载原编码、姓名、职务
        $("#code").html(data[0].code);
        $("#name").html(data[0].name);
        //获取部门信息
        var dept = fnGetSingleData("t_base_department", "id='" + data[0].dept_id + "'");
        if (formcode == "1") {
            var duty = fnGetSingleData("t_base_duty", "id='" + data[0].duty_id + "'");
            var post = fnGetSingleData("t_base_post", "duty_id='" + data[0].duty_id + "' and department_id='" + data[0].dept_id + "'");
            $("#oldduty").html("【" + dept[0].name + "～" + duty[0].name + "】");
            //原职位ID
            oldDutyId = post[0].id;
        }
        else if (formcode == "4") {
            var type = fnGetSingleData("v_data_base_dictionary_detail", "[code]='hr_yuangongleixing' and [value]='" + data[0].type+ "'", 0);
            $("#oldtype").html(type[0].name);
            oldType = type[0].name;
        }
        else {
            $("#oldDepartment").html("【" + dept[0].name + "】");

        }


    }
};

//部门valuechanged触发
function onDeptChanged(e) {
    var dept_id = mini.get("dept_id");
    var duty_id = mini.get("duty_id");
    var id = dept_id.getValue();
    if (id != "null") {
        duty_id.load("/extendpage/data/jsondata.aspx?entity=v_t_base_duty&type=1&condition=dept_id='" + id + "'");
        //新增时默认第一个
        if (instance.duty_id == "") {
            duty_id.select(0);
        } else {
            function Duty(data) {
                var json = mini.decode(data);
                for (var i = 0; i < json.length; i++) {
                    //编辑时选中职务
                    if (json[i].id == instance.duty_id) {
                        duty_id.select(i);
                        break;
                    }

                }
            }
        }
    }
    //加载当前部门下的职务
    getAjax("/extendpage/data/jsondata.aspx?entity=v_t_base_duty&type=1&condition=dept_id='" + id + "'", null, Duty);

}

//自定义保存
var fnCustomSave = function () {
    if (formcode == "0") {
        updateCode();//工号变更
    }
    else if (formcode == "1") {
        updateDuty();//职务变更
    }
    else if (formcode == "2") {
        updateName();//姓名变更
    }
    else if (formcode == "3") {
        updateLeave();//员工离职
    }
    else {
        updateType();//类型变更
    }

}

//工号调整
var updateCode = function () {
    var form = new mini.Form("editForm1");
    form.validate();
    if (!form.isValid()) return;
    var form_data = form.getData();
    if (form_data.newcode == "") {
        alert("请输入新的员工号！");
        return;
    }
    form_data.employeeid = getPar("eid");
    form_data.modifytype = 0;
    form_data.oldState = $("#code").html();
    form_data.newState = form_data.newcode;
    form_data.effectDate = mini.formatDate(form_data.effectDate, "yyyy-MM-dd");
    fnClone(form_data, instance);

    var instanceEmployee = fnGetEntityInstance("t_base_employee", "id='" + getPar("eid") + "'");
    instanceEmployee.code = form_data.newcode;

    if (typeof (instance) != "undefined" && instance != null) {
        var json = mini.encode(instance);//序列化成JSON
        $.ajax({
            url: "/extendpage/action/datasave.aspx?entity=t_employee_modifyInfo&flag=1&action=insert",
            type: "post",
            data: { data: json },
            cache: false,
            success: function (text) {
                if (text == 1) {
                    //更新客户档案表
                    var employeeJson = mini.encode(instanceEmployee);
                    $.ajax({
                        url: "/extendpage/action/datasave.aspx?entity=t_base_employee&flag=1&action=update",
                        type: "post",
                        data: { data: employeeJson },
                        cache: false,
                        success: function (text) {
                            if (text == 1) {
                                alert("数据保存成功!")
                                fnClose();
                            }
                        },
                        error: function (jqXHR, textStatus, errorThrown) {
                            alert(jqXHR.responseText);
                        }
                    });
                }
            },
            error: function (jqXHR, textStatus, errorThrown) {
                alert(jqXHR.responseText);
            }
        });
    }

}


//更新姓名信息到员工信息变更表中
var updateName = function () {
    var form = new mini.Form("form1");
    form.validate();
    if (!form.isValid()) return;
    var form_data = form.getData();
    if (form_data.newname == "") {
        alert("请输入员工新姓名！");
        return;
    }
    form_data.employeeid = getPar("eid");
    form_data.modifytype = 2;
    form_data.oldState = $("#name").html();
    form_data.newState = form_data.newname;
    form_data.effectDate = mini.formatDate(form_data.effectDate, "yyyy-MM-dd");
    fnClone(form_data, instance);

    var instanceEmployee = fnGetEntityInstance("t_base_employee", "id='" + getPar("eid") + "'");
    instanceEmployee.name = form_data.newname;

    if (typeof (instance) != "undefined" && instance != null) {
        var json = mini.encode(instance);//序列化成JSON
        $.ajax({
            url: "/extendpage/action/datasave.aspx?entity=t_employee_modifyInfo&flag=1&action=insert",
            type: "post",
            data: { data: json },
            cache: false,
            success: function (text) {
                if (text == 1) {
                    //更新客户档案表
                    var employeeJson = mini.encode(instanceEmployee);
                    $.ajax({
                        url: "/extendpage/action/datasave.aspx?entity=t_base_employee&flag=1&action=update",
                        type: "post",
                        data: { data: employeeJson },
                        cache: false,
                        success: function (text) {
                            if (text == 1) {
                                alert("数据保存成功!")
                                fnClose();
                            }
                        },
                        error: function (jqXHR, textStatus, errorThrown) {
                            alert(jqXHR.responseText);
                        }
                    });
                }
            },
            error: function (jqXHR, textStatus, errorThrown) {
                alert(jqXHR.responseText);
            }
        });
    }
}


//更改职务信息到员工信息变更表中
var updateDuty = function () {
    //数据校验
    var form = new mini.Form("form1");
    form.validate();
    if (!form.isValid()) return;
    var form_data = form.getData();
    if (form_data.duty_id == "") {
        alert("请选择员工新的职务！");
        return;
    }
    //拼结数据
    form_data.employeeid = getPar("eid");
    form_data.modifytype = 1;
    form_data.oldState = oldDutyId;
    //新的职位ID 
    var post = fnGetSingleData("t_base_post", "duty_id='" + form_data.duty_id + "' and department_id='" + form_data.dept_id + "'");
    form_data.newState = post[0].id;
    form_data.effectDate = mini.formatDate(form_data.effectDate, "yyyy-MM-dd");
    fnClone(form_data, instance);

    var instanceEmployee = fnGetEntityInstance("t_base_employee", "id='" + getPar("eid") + "'");
    instanceEmployee.dept_id = form_data.dept_id;
    instanceEmployee.duty_id = form_data.duty_id;

    if (typeof (instance) != "undefined" && instance != null) {
        var json = mini.encode(instance);//序列化成JSON
        $.ajax({
            url: "/extendpage/action/datasave.aspx?entity=t_employee_modifyInfo&flag=1&action=insert",
            type: "post",
            data: { data: json },
            cache: false,
            success: function (text) {
                if (text == 1) {
                    //更新客户档案表
                    var employeeJson = mini.encode(instanceEmployee);
                    $.ajax({
                        url: "/extendpage/action/datasave.aspx?entity=t_base_employee&flag=1&action=update",
                        type: "post",
                        data: { data: employeeJson },
                        cache: false,
                        success: function (text) {
                            if (text == 1) {
                                alert("数据保存成功!")
                                fnClose();
                            }
                        },
                        error: function (jqXHR, textStatus, errorThrown) {
                            alert(jqXHR.responseText);
                        }
                    });
                }
            },
            error: function (jqXHR, textStatus, errorThrown) {
                alert(jqXHR.responseText);
            }
        });
    }

}

//员工离职
var updateLeave = function () {
    var form = new mini.Form("form1");
    form.validate();
    if (!form.isValid()) return;
    var form_data = form.getData();
    form_data.employeeid = getPar("eid");
    form_data.modifytype = 3;
    form_data.oldState = "在职";
    form_data.newState = "离职";
    form_data.effectDate = mini.formatDate(form_data.effectDate, "yyyy-MM-dd");
    fnClone(form_data, instance);

    var instanceEmployee = fnGetEntityInstance("t_base_employee", "id='" + getPar("eid") + "'");
    instanceEmployee.status = 3;
    instanceEmployee.leave_time = form_data.effectDate;
    if (typeof (instance) != "undefined" && instance != null) {
        var json = mini.encode(instance);//序列化成JSON
        $.ajax({
            url: "/extendpage/action/datasave.aspx?entity=t_employee_modifyInfo&flag=1&action=insert",
            type: "post",
            data: { data: json },
            cache: false,
            success: function (text) {
                if (text == 1) {
                    //更新客户档案表
                    var employeeJson = mini.encode(instanceEmployee);
                    $.ajax({
                        url: "/extendpage/action/datasave.aspx?entity=t_base_employee&flag=1&action=update",
                        type: "post",
                        data: { data: employeeJson },
                        cache: false,
                        success: function (text) {
                            if (text == 1) {
                                alert("数据保存成功!")
                                fnClose();
                            }
                        },
                        error: function (jqXHR, textStatus, errorThrown) {
                            alert(jqXHR.responseText);
                        }
                    });
                }
            },
            error: function (jqXHR, textStatus, errorThrown) {
                alert(jqXHR.responseText);
            }
        });
    }

}


//员工类型变更
var updateType = function () {
    var form = new mini.Form("form1");
    form.validate();
    if (!form.isValid()) return;
    var form_data = form.getData();
    form_data.employeeid = getPar("eid");
    form_data.modifytype = 4;
    form_data.oldState = oldType;
    //根据类型编号取得类型名称
    var type = fnGetSingleData("v_data_base_dictionary_detail", "[code]='hr_yuangongleixing' and [value]='02'", 0);
    form_data.newState = type[0].name;
    form_data.effectDate = mini.formatDate(form_data.effectDate, "yyyy-MM-dd");
    fnClone(form_data, instance);

    var instanceEmployee = fnGetEntityInstance("t_base_employee", "id='" + getPar("eid") + "'");
    instanceEmployee.type = form_data.type;
    if (typeof (instance) != "undefined" && instance != null) {
        var json = mini.encode(instance);//序列化成JSON
        $.ajax({
            url: "/extendpage/action/datasave.aspx?entity=t_employee_modifyInfo&flag=1&action=insert",
            type: "post",
            data: { data: json },
            cache: false,
            success: function (text) {
                if (text == 1) {
                    //更新客户档案表
                    var employeeJson = mini.encode(instanceEmployee);
                    $.ajax({
                        url: "/extendpage/action/datasave.aspx?entity=t_base_employee&flag=1&action=update",
                        type: "post",
                        data: { data: employeeJson },
                        cache: false,
                        success: function (text) {
                            if (text == 1) {
                                alert("数据保存成功!")
                                fnClose();
                            }
                        },
                        error: function (jqXHR, textStatus, errorThrown) {
                            alert(jqXHR.responseText);
                        }
                    });
                }
            },
            error: function (jqXHR, textStatus, errorThrown) {
                alert(jqXHR.responseText);
            }
        });
    }

}